package com.hema.auction.widget.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hema.auction.R;
import com.hema.auction.activity.MessageActivity;
import com.hema.auction.utils.manager.SPManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * author by john
 * 自定义title布局
 */
public class TitleView extends LinearLayout {

    private Context context;
    @BindView(R.id.ll_root)
    RelativeLayout llRoot;
    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rl_message)
    RelativeLayout rlMessage;
    @BindView(R.id.iv_message)
    ImageView ivMessage;
    @BindView(R.id.tv_new_message)
    TextView tvNewMessage;
    @BindView(R.id.iv_center)
    ImageView ivCenter;
    @BindView(R.id.line)
    View line;

    private String title, rightText;
    private Drawable centerImg, leftImg, rightImg;
    private int titleBg = -1, rightTextCoolor = -1;
    private boolean showLeft, showMessage, isLeftBack;

    public TitleView(Context context) {
        super(context);
        init(context, null);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        ButterKnife.bind(this, LayoutInflater.from(context).inflate(R.layout.layout_title, this));
        TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.ViewTitle, 0, 0);
        title = a.getString(R.styleable.ViewTitle_title_name);
        rightText = a.getString(R.styleable.ViewTitle_right_text);
        centerImg = a.getDrawable(R.styleable.ViewTitle_center_img);
        leftImg = a.getDrawable(R.styleable.ViewTitle_left_img);
        rightImg = a.getDrawable(R.styleable.ViewTitle_right_img);
        showLeft = a.getBoolean(R.styleable.ViewTitle_show_left, true);
        showMessage = a.getBoolean(R.styleable.ViewTitle_show_message, true);
        titleBg = a.getColor(R.styleable.ViewTitle_title_bg, getResources().getColor(R.color.white));
        isLeftBack = a.getBoolean(R.styleable.ViewTitle_left_back, true);
        rightTextCoolor = a.getColor(R.styleable.ViewTitle_right_text_color, getResources().getColor(R.color.white));
        if (centerImg != null) {
            tvTitle.setBackground(centerImg);
            ivCenter.setImageDrawable(centerImg);
            ivCenter.setVisibility(VISIBLE);
            tvTitle.setVisibility(GONE);
        } else {
            ivCenter.setVisibility(GONE);
            tvTitle.setVisibility(VISIBLE);
            tvTitle.setText(title);
        }
        llRoot.setBackgroundColor(titleBg);
        if (titleBg == getResources().getColor(R.color.white)) {
            tvTitle.setTextColor(getResources().getColor(R.color.black33));
        } else {
            line.setVisibility(GONE);
            tvTitle.setTextColor(getResources().getColor(R.color.white));
        }
        if (showLeft) {
            ivLeft.setVisibility(VISIBLE);
            if (leftImg == null) {
                ivLeft.setImageResource(R.drawable.ic_back);
            } else {
                ivLeft.setImageDrawable(leftImg);
            }
        } else {
            ivLeft.setVisibility(GONE);
        }

        if (showMessage) {
            if (rightImg != null)
                ivMessage.setImageDrawable(rightImg);

            rlMessage.setVisibility(VISIBLE);
            tvRight.setVisibility(GONE);
            int messageCount = SPManager.getInstance(context).getNewMessageCount();
            if (messageCount > 0)
                tvNewMessage.setVisibility(VISIBLE);
            else
                tvNewMessage.setVisibility(GONE);
        } else {
            if (!TextUtils.isEmpty(rightText)) {
                tvRight.setVisibility(VISIBLE);
                tvRight.setText(rightText);
                tvRight.setTextColor(rightTextCoolor);
            } else {
                tvRight.setVisibility(GONE);
            }
            rlMessage.setVisibility(GONE);
        }

        a.recycle();
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == VISIBLE) {
            int messageCount = SPManager.getInstance(context).getNewMessageCount();
            if (messageCount > 0)
                tvNewMessage.setVisibility(VISIBLE);
            else
                tvNewMessage.setVisibility(GONE);
        }
    }

    @OnClick({R.id.iv_left, R.id.rl_message})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                if (isLeftBack)
                    ((Activity) context).finish();
                break;
            case R.id.rl_message:
                Intent intent = new Intent(context, MessageActivity.class);
                context.startActivity(intent);
                break;
        }

    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public String getTitle() {
        return tvTitle.getText().toString();
    }

    public TextView getTitleView() {
        return tvTitle;
    }

    public void setTitle(int resId) {
        tvTitle.setText(resId);
    }

    public void setRightText(int resId) {
        tvRight.setText(resId);
    }

    public void setRightText(String text) {
        tvRight.setText(text);
    }

    public View getLeftView() {
        return ivLeft;
    }

    public View getRightView() {
        return tvRight;
    }

    public View getRightImageVIew() {
        return ivMessage;
    }

    public void setLeftImage(int resId) {
        ivLeft.setImageResource(resId);
    }

    public void setLeftViewIsVisible(boolean isVisible) {
        showLeft = isVisible;
        if (showLeft)
            ivLeft.setVisibility(View.VISIBLE);
        else
            ivLeft.setVisibility(View.GONE);

    }

    public void setNewMessageBg(int resId) {
        tvNewMessage.setBackgroundResource(resId);
    }


}
